<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/images/CTlogo.svg" alt />
        <h1>中国电信某某分公司IT管理系统</h1>
      </div>
      <el-button size="mini" type="info" @click="logout">EXIT</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '68px' :'200px'">
        <div class="toggle-button" @click="toggleCollapse">
          <span v-show="!isCollapse">版本ver:1.0</span>
          <i :class="isCollapse ? 'el-icon-s-fold' : 'el-icon-s-unfold'"></i>
        </div>
        <el-menu
          :collapse="isCollapse"
          router
          :default-active="activePath"
          :collapse-transition="false"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409bff"
        >
          <!-- 一级菜单 -->
          <el-submenu v-for="menu in menuList" :index="'/'+menu.path" :key="menu.id">
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <i :class="iconsObj[menu.id]"></i>
              <span>{{menu.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              @click="saveNavState('/'+menu2.path)"
              v-for="menu2 in menu.children"
              :index="'/'+menu2.path"
              :key="menu2.id"
            >
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>{{menu2.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容主体区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      // 用于存储菜单列表数据
      menuList: [],
      // 用于存储id与图标的对应关系
      iconsObj: {
        125: 'el-icon-location',
        103: 'el-icon-s-tools',
        101: 'el-icon-s-order',
        102: 'el-icon-menu',
        145: 'el-icon-s-help'
      },
      isCollapse: false,
      // 被激活的链接
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
      this.$message.success('退出系统成功！')
    },
    // 异步获取菜单数据 并赋值给menuList
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error('res.meta.msg')
      // console.log('res', res)
      this.menuList = res.data
      this.$message.success('获取成功')
    },
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 保存激活状态
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scope>
.home-container {
  height: 100%;
  .el-header {
    background-color: #373d41;
    color: #fff;
    display: flex;
    justify-content: space-between;
    align-items: center;

    div {
      width: 600px;
      display: flex;
      align-items: center;
      img {
        max-height: 36px;
      }
      h1 {
        font-weight: 400;
        font-size: 1.2em;
        margin-left: 0.5em;
      }
    }
  }
  .el-aside {
    background-color: #333744;
    transition: all 0.5s;
    .toggle-button {
      // height: 45px;
      padding: 10px;
      background-color: #4a5064;
      font-size: 0.85em;
      color: #ddd;
      display: flex;
      justify-content: center;
      span {
        margin-right: 3em;
      }
      i {
        font-size: 25px;
      }
    }
    .el-menu {
      border: none;
    }
  }
  .el-main {
    background-color: #eaedf1;
  }
}
</style>
